package com.xyht.sca_s.student_manage_system.modules.attendance.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.common.util.DynamicSchedulingTaskUtil;
import com.xyht.sca_s.student_manage_system.common.util.TimeUtil;
import com.xyht.sca_s.student_manage_system.modules.attendance.entity.req.SmsAttendanceReq;
import com.xyht.sca_s.student_manage_system.modules.attendance.service.SmsAttendanceService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 考勤打卡 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-02-23
 */
@RestController
@RequestMapping("/attendance")
@SecretBody
public class SmsAttendanceController {
    @Autowired
    private SmsAttendanceService smsAttendanceService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private DynamicSchedulingTaskUtil dynamicSchedulingTaskUtil;

    @ApiModelProperty(value = "考勤打卡任务创建")
    @PostMapping("/task/add")
    public ResponseResult createAttendance(@RequestBody SmsAttendanceReq smsAttendanceReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAttendanceService.createAttendance(smsAttendanceReq, user_id);
    }

    @ApiModelProperty(value = "考勤打卡任务修改")
    @PostMapping("/task/update")
    public ResponseResult updateAttendance(@RequestBody SmsAttendanceReq smsAttendanceReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAttendanceService.updateAttendance(smsAttendanceReq, user_id);
    }

    @ApiModelProperty(value = "考勤打卡任务关闭")
    @PostMapping("/task/close")
    public ResponseResult closeAttendance(@RequestBody SmsAttendanceReq smsAttendanceReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAttendanceService.closeAttendance(smsAttendanceReq,user_id);
    }

    @ApiModelProperty(value = "学生定位检测（定位人数小于六人无法检测）")
    @PostMapping("/task/check")
    public ResponseResult checkOutlierStudent(@RequestBody SmsAttendanceReq smsAttendanceReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAttendanceService.checkOutlierStudent(smsAttendanceReq,user_id);
    }

    @ApiModelProperty(value = "本人考勤打卡事件下拉框")
    @GetMapping("/task/eventList")
    public ResponseResult getHistoryAttendanceEvent(  @RequestParam(value = "search_str", required = false) String search_str,
                                                      HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAttendanceService.getHistoryAttendanceEvent(search_str,user_id);
    }

    @ApiModelProperty(value = "考勤打卡任务列表")
    @GetMapping("/task/list")
    public ResponseResult getAttendanceList(HttpServletRequest request,
                                            @RequestParam(value = "search_str", required = false) String search_str,
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "bg_date", required = false) String bg_date,
                                            @RequestParam(value = "end_date", required = false) String end_date,
                                            @RequestParam(value = "event_name", required = false) String event_name,
                                            @RequestParam(value = "status", required = false) Integer status
    ) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAttendanceService.getAttendanceList(pageNum,pageSize,bg_date,end_date,user_id,event_name,status,search_str);
    }

    @ApiModelProperty(value = "考勤打卡任务详情")
    @GetMapping("/task/detail")
    public ResponseResult getAttendanceDetail(HttpServletRequest request,
                                            @RequestParam(value = "attendance_id") String attendance_id
    ) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAttendanceService.getAttendanceDetail(attendance_id,user_id);
    }

    @SecretBody
    @ApiModelProperty(value = "考勤打卡任务测试")
    @PostMapping("/task/test")
    public ResponseResult test(@RequestBody Map name) {
        return dynamicSchedulingTaskUtil.createTask((String) name.get("name"),"test", TimeUtil.AddSeconds(new Date(),10));
    }

}

